﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExcelLibrary.SpreadSheet;

namespace NetworkSupervisor.Helper
{
    public class ExcelHelper : IExcelHelper
    {
        private Workbook workBook = null;
        private Worksheet workSheet = null;

        public int RowCount
        {
            get { return workSheet.Cells.Rows.Count; }
        }

        public int ColumnCount
        {
            get
            {
                int i = 0;
                while (GetCellValue(0, i) != "") i++;
                return i;
            }
        }

        public IList<string> ColumnList
        {
            get
            {
                IList<string> columnList = new List<string>();
                for (int columnIndex = 0; columnIndex < ColumnCount; columnIndex++)
                {
                    columnList.Add(GetCellValue(0, columnIndex));
                }
                return columnList;
            }
        }

        public ExcelHelper(string file)
        {
            workBook = Workbook.Load(file);
            workSheet = workBook.Worksheets[0];
        }

        public string GetCellValue(int rowIndex, int columnIndex)
        {
            return workSheet.Cells[rowIndex, columnIndex].StringValue;
        }

        public string GetCellValue(int rowIndex, string columnName)
        {
            int columnIndex = ColumnList.IndexOf(columnName);
            object value = workSheet.Cells[rowIndex, columnIndex].Value;
            if (value != null)
            {
                return value.ToString();
            }
            return "";
        }
    }
}